﻿using System.Configuration;

namespace XYAuto.ChiTu.Config
{
    public class BaseConfigCollection<T> : ConfigurationElementCollection
        where T : ItemConfig, new()
    {
        public T this[int index]
        {
            get { return (T)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        public new T this[string keyValue]
        {
            get { return (T)BaseGet(keyValue); }
        }

        public void Add(T appConfig)
        {
            BaseAdd(appConfig);
        }

        public void Clear()
        {
            BaseClear();
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new T();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((T)element).AppKey;
        }

        public void Remove(T appConfig)
        {
            BaseRemove(appConfig.AppKey);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }
    }
}